package com.cr.admin.business.vip.dao.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.cr.admin.business.topic.pojo.entity.Topic;
import com.cr.admin.business.vip.dao.mapper.VipMapper;
import com.cr.admin.business.vip.pojo.dto.VipAddParam;
import com.cr.admin.business.vip.pojo.dto.VipPasswordParam;
import com.cr.admin.business.vip.pojo.dto.VipQuery;
import com.cr.admin.business.vip.pojo.dto.VipUpdateParam;
import com.cr.admin.business.vip.pojo.entity.Vip;
import com.cr.admin.system.pojo.entity.SysUser;
import com.cr.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public class VipRepositoryImpl implements VipRepository{
    @Autowired
    private VipMapper vipMapper;

    @Override
    public List<Vip> selectList(VipQuery vipQuery) {
        QueryWrapper<Vip> queryWrapper= new QueryWrapper<>();
        if(vipQuery.getPhoneNum()!=null){
            queryWrapper.like("phone_num",vipQuery.getPhoneNum());
        }
        if(vipQuery.getNickname()!=null){
            queryWrapper.like("nickname",vipQuery.getNickname());
        }
        return vipMapper.selectList(queryWrapper);
    }

    @Override
    public void insert(VipAddParam vipAddParam) {
        Vip vip = new Vip();
        BeanUtils.copyProperties(vipAddParam,vip);
        vipMapper.insert(vip);
    }

    @Override
    public void updateById(VipUpdateParam vipUpdateParam) {
        Vip vip = new Vip();
        BeanUtils.copyProperties(vipUpdateParam,vip);
        vipMapper.updateById(vip);
    }

    @Override
    public Vip selectById(Long id) {
        return vipMapper.selectById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        vipMapper.deleteBatchIds(ids);
    }

    @Override
    public void resetPassword(Long id) {
        Vip vip = new Vip();
        vip.setId(id);
        vip.setPassword(BCrypt.hashpw("123456"));
        vipMapper.updateById(vip);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Vip vip = new Vip();
        vip.setId(id);
        vip.setStatus(status);
        vipMapper.updateById(vip);
    }


}
